﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNdotnet.Ezsocio.Core.Common;
using CNdotnet.Ezsocio.DependenceInjection;

namespace CNdotnet.Ezsocio.Core.Service
{
    public abstract class ServiceBase
    {
        #region Repository Manager

        public TRepository LoadRepository<TRepository>()
        {
            IContainerContext container = ContainerManager.GetContainer("repositoryContainer");
            return container.Resolve<TRepository>();
        }

        #endregion

        #region Service Manager

        public TService LoadService<TService>()
        {
            IContainerContext container = ContainerManager.GetContainer("serviceContainer");
            return container.Resolve<TService>();
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs before updated.
        /// </summary>
        public event EventHandler<SavedEventArgs> BeforeUpdated;

        /// <summary>
        /// Occurs after updated
        /// </summary>
        public event EventHandler<SavedEventArgs> AfterUpdated;

        /// <summary>
        /// Occurs when save failed
        /// </summary>
        public event EventHandler<FailedEventArgs> UpdateFailed;

        #endregion

        #region Protected Virtual Methods

        /// <summary>
        /// Called before updated
        /// </summary>
        /// <param name="obj">The obj.</param>
        protected virtual void OnBeforeUpdated(Object obj, SaveAction action)
        {
            EventHandler<SavedEventArgs> tempHandler = BeforeUpdated;
            if (tempHandler != null)
            {
                tempHandler(obj, new SavedEventArgs(obj, action));
            }
        }

        /// <summary>
        /// Called after updated
        /// </summary>
        /// <param name="obj">The obj.</param>
        protected virtual void OnAfterUpdated(Object obj, SaveAction action)
        {
            EventHandler<SavedEventArgs> tempHandler = AfterUpdated;
            if (tempHandler != null)
            {
                tempHandler(obj, new SavedEventArgs(obj, action));
            }
        }

        /// <summary>
        /// Called when save failed
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="exception">The exception.</param>
        protected virtual void OnSaveFailed(Object sender, Exception exception)
        {
            EventHandler<FailedEventArgs> tempHandler = UpdateFailed;
            if (tempHandler != null)
            {
                tempHandler(sender, new FailedEventArgs(sender, exception));
            }
        }
        #endregion
    }
}
